﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 严格来说，这里不算是截屏，只是单纯在给固定图片在固定位置添加游戏水印和二维码
/// </summary>
public class ScreenCapture : MonoBehaviour
{
    public Texture2D backTextrue;
    public Texture2D upTeture;
    public RawImage rawImg;

    public enum WaterMarkConrner
    {
        uperLeft,
        uperRight,
        lowerLeft,
        lowerRight
    }
    

    // Start is called before the first frame update
    void Start()
    {
        Vector2 dis = new Vector2(1, 1);
        rawImg.texture = TextureOverlay(backTextrue, upTeture, WaterMarkConrner.lowerRight, dis);
        SaveTextureToFile((Texture2D)rawImg.texture, "share.png");
    }

    /// <summary>
    /// </summary>
    /// <param name="backTextrue">背景图</param>
    /// <param name="path">需要覆盖上的图</param>
    /// <param name="w">添加的位置</param>
    /// <param name="dis">dis 为图片的对应conrner 与 conrner的距离 若WaterMarkConrner 选的是lowerright 
    /// 则是地板图的右下角距离覆盖图的右下角的像素距离</param>
    /// <returns></returns>
    public static Texture2D TextureOverlay(Texture2D backTextrue, Texture2D upTeture, WaterMarkConrner w, Vector2 dis)
    {
        Color[] colors = upTeture.GetPixels();
        int startX = 0, starty = 0;
        switch (w)
        {
            case WaterMarkConrner.lowerLeft:
                startX = 0 + (int)dis.x; starty = 0 + (int)dis.y;
                break;
            case WaterMarkConrner.lowerRight:
                startX = backTextrue.width - upTeture.width - (int)dis.x; starty = 0 + (int)dis.y;
                break;
            case WaterMarkConrner.uperLeft:
                startX = 0 + (int)dis.x; starty = backTextrue.height - upTeture.height - (int)dis.y;
                break;
            case WaterMarkConrner.uperRight:
                startX = backTextrue.width - upTeture.width - (int)dis.x; starty = backTextrue.height - upTeture.height - (int)dis.y;
                break;
        }

        for (int i = 0; i < upTeture.width; i++)
        {
            for (int j = 0; j < upTeture.height; j++)
            {
                int x = i + startX, y = j + starty;

                if (upTeture.GetPixel(i, j).a == 0)
                {
                    Color c = backTextrue.GetPixel(x, y);
                    colors[upTeture.width * j + i] = c;
                }

                // 透明通道添加
                else if (upTeture.GetPixel(i, j).a < 0.5)
                {
                    Color c = backTextrue.GetPixel(x, y);
                    colors[upTeture.width * j + i] = (c * c.a + upTeture.GetPixel(i, j) * (1 - (upTeture.GetPixel(i, j).a))) / 2;
                }
            }
        }
        backTextrue.SetPixels(startX, starty, upTeture.width, upTeture.height, colors);
        backTextrue.Apply();
        return backTextrue;
    }

    public void SaveTextureToFile(Texture2D texture, string fileName)
    {
        var bytes = texture.EncodeToPNG();
        var file = File.Open(Application.dataPath + "/" + fileName, FileMode.Create);
        var binary = new BinaryWriter(file);
        binary.Write(bytes);
        file.Close();
    }
}




